/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.info.controller  
 */
package com.apache.info.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.apache.action.PlatFromAction;
import com.apache.api.api.ApiService;
import com.apache.api.manager.BaseManager;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.api.vo.ResultMsg;
import com.apache.database.constant.SystemTools;
import com.apache.database.constant.Validator;
import com.apache.database.model.Page;
import com.apache.info.manager.PubMetadataManager;
import com.apache.tools.DateUtils;
import com.apache.tools.DateUtils.FmtStr;
import com.apache.tools.StrUtil;
import com.apache.uct.common.LoginUser;

/**
 * description:  管理端信息操作action
 * @author dwusoft 创建时间：2016-03-14  
 * @Copyright(c)2014:北京--软件技术有限公司
 */
@Controller
@RequestMapping("/info/admin/")
public class AdminAction extends PlatFromAction {

	@Autowired
	private ApiService infoService;
	@Autowired
	private PubMetadataManager pubMetadataManager;

	/**
	 * 功能说明：通用列表查询页面跳转
	 */
	@RequestMapping("toPage.action")
	public ModelAndView toPage(HttpServletRequest request) {
		String toPage = request.getParameter("toPage");
		ModelAndView view = new ModelAndView("info/info-list-" + toPage);
		return view;
	}

	/**
	 * 通用列表信息数据查询
	 */
	@RequestMapping("topage!data.action")
	@ResponseBody
	protected Object topagedata(HttpServletRequest request, String page, String rows) {
		String sortCode = request.getParameter("sortCode");
		ParamsVo vo = new ParamsVo();
		vo.setParams("pageIndex", Validator.getDefaultStr(page, "1"));
		vo.setParams("pageSize", Validator.getDefaultStr(rows, "10"));
		vo.setParams("sortCode", sortCode);
		vo.setParams("orderby", "create_time desc");
		vo.setMethodKey("selectInfo");
		vo.setParams("sysPass", SystemTools.getInstance().getValue("push_to_dwusoft_pass"));
		ResultEntity entity = infoService.doService(vo);
		Map<String, Object> mapData = new HashMap<String, Object>();
		if ("true".equals(entity.getResult()) && null != entity.getEntity()) {
			Page pageList = (Page) entity.getEntity();
			mapData.put("total", pageList.getCount());
			mapData.put("rows", pageList.getPageObjects());
		}
		return mapData;
	}

	/**
	 * 功能说明：通用列表查询页面-定制化查询
	 */
	@RequestMapping("list-all.action")
	public ModelAndView listAll(HttpServletRequest request) {
		ModelAndView view = new ModelAndView("info/info-all-list");
		return view;
	}

	/**
	 * 信息综合查询
	 */
	@RequestMapping("list-all!data.action")
	@ResponseBody
	protected Object data(HttpServletRequest request, String page, String rows) {
		String sortCodeStr = request.getParameter("sortCodeStr");
		String[] scs = sortCodeStr.split(",");
		StringBuffer sqlStr = new StringBuffer("select * from ( ");
		for (int i = 0; i < scs.length; i++) {
			String str = scs[i];
			sqlStr.append("select  '"
					+ str
					+ "' as sort_code,info_id,info_title,info_type,is_top,is_top_time, status,create_time,create_user from pub_"
					+ str);
			if (i < scs.length - 1) {
				sqlStr.append(" union ");
			}
		}
		sqlStr.append(" ) t ");
		sqlStr.append(" where 1=1 ");
		if (StrUtil.isNotNull(String.valueOf(request.getParameter("createUser")))) {
			sqlStr.append(" and t.create_user='" + String.valueOf(request.getParameter("createUser")) + "' ");
		}
		if (StrUtil.isNotNull(String.valueOf(request.getParameter("infoTitle")))) {
			sqlStr.append(" and t.info_title like '%" + String.valueOf(request.getParameter("infoTitle")).trim()
					+ "%' ");
		}
		if (StrUtil.isNotNull(String.valueOf(request.getParameter("status")))) {
			sqlStr.append(" and t.status = '" + String.valueOf(request.getParameter("status")) + "' ");
		}
		sqlStr.append(" order by t.is_top_time desc");
		System.out.println(sqlStr.toString());
		ParamsVo vo = new ParamsVo();
		vo.setParams("pageIndex", Validator.getDefaultStr(page, "1"));
		vo.setParams("pageSize", Validator.getDefaultStr(rows, "10"));
		vo.setParams("dynamicSql", sqlStr.toString());
		vo.setParams("sortCode", "t_zl_info");//任意存在的表，因为传入dynamicSql参数的，实际以dynamicSql执行为准
		vo.setMethodKey("selectInfo");
		vo.setParams("sysPass", SystemTools.getInstance().getValue("push_to_dwusoft_pass"));
		ResultEntity entity = infoService.doService(vo);
		Map<String, Object> mapData = new HashMap<String, Object>();
		if ("true".equals(entity.getResult()) && null != entity.getEntity()) {
			Page pageList = (Page) entity.getEntity();
			mapData.put("total", pageList.getCount());
			mapData.put("rows", pageList.getPageObjects());
		}
		request.setAttribute("sortId", request.getParameter("sortId"));
		return mapData;
	}

	/**
	 * 信息状态修改
	 * @return
	 */
	@RequestMapping("upStatus.action")
	@ResponseBody
	protected ResultMsg upStatus(HttpServletRequest request) {
		String sortCode = request.getParameter("sortCode");
		String infoId = request.getParameter("infoId");
		String status = request.getParameter("status");
		ResultMsg rsm = new ResultMsg("F", "操作失败");

		ParamsVo vo = new ParamsVo();
		vo.setParams("sortCode", sortCode);
		vo.setParams("infoId", infoId);
		vo.setParams("status", status);
		vo.setMethodKey("updateInfo");
		vo.setParams("sysPass", SystemTools.getInstance().getValue("push_to_dwusoft_pass"));
		ResultEntity entity = infoService.doService(vo);
		if ("true".equals(entity.getResult()) && "true".equals(String.valueOf(entity.getEntity()))) {
			rsm = new ResultMsg("T", "操作成功");
		}
		return rsm;
	}

	/**
	 * 信息删除
	 * @return
	 */
	@RequestMapping("delInfo.action")
	@ResponseBody
	protected ResultMsg delInfo(HttpServletRequest request) {
		String sortCode = request.getParameter("sortCode");
		String infoId = request.getParameter("infoId");
		ResultMsg rsm = new ResultMsg("F", "操作失败");

		ParamsVo vo = new ParamsVo();
		vo.setParams("sortCode", sortCode);
		vo.setParams("infoId", infoId);
		vo.setMethodKey("deleteInfo");
		vo.setParams("sysPass", SystemTools.getInstance().getValue("push_to_dwusoft_pass"));
		ResultEntity entity = infoService.doService(vo);
		if ("true".equals(entity.getResult()) && "true".equals(String.valueOf(entity.getEntity()))) {
			rsm = new ResultMsg("T", "操作成功");
		}
		return rsm;
	}

	/**
	 * 信息置顶推荐/撤销推荐
	 * @param request
	 * @return
	 */
	@RequestMapping("isTop.action")
	@ResponseBody
	protected ResultMsg isTop(HttpServletRequest request) {
		String sortCode = request.getParameter("sortCode");
		String infoId = request.getParameter("infoId");
		String isTop = request.getParameter("isTop");
		ResultMsg rsm = new ResultMsg("F", "操作失败");

		ParamsVo vo = new ParamsVo();
		vo.setParams("sortCode", sortCode);
		vo.setParams("infoId", infoId);
		vo.setParams("isTop", StrUtil.doNull(isTop, "F"));
		if ("T".equals(isTop)) {
			vo.setParams("isTopTime", DateUtils.format(new Date(), FmtStr.yyyyMMdd_HHmmss.toString()));
		}
		vo.setMethodKey("updateInfo");
		vo.setParams("sysPass", SystemTools.getInstance().getValue("push_to_dwusoft_pass"));
		ResultEntity entity = infoService.doService(vo);
		if ("true".equals(entity.getResult()) && "true".equals(String.valueOf(entity.getEntity()))) {
			rsm = new ResultMsg("T", "操作成功");
		}
		return rsm;
	}

	@RequestMapping("addMessge.action")
	@ResponseBody
	protected ResultMsg addMessge(HttpServletRequest request) {
		String sortCode = "notice";
		Map<String, String> pMap = this.getParameterMap(request);
		ResultMsg rsm = new ResultMsg("F", "操作失败");
		ParamsVo vo = new ParamsVo();
		vo.setParams("sortCode", sortCode);
		LoginUser loginUser = (LoginUser) request.getSession().getAttribute("loginUser");
		if (!Validator.isEmpty(loginUser)) {
			vo.setParams("noticeId", UUID.randomUUID().toString().replaceAll("-", ""));
			vo.setParams("createUser", loginUser.getUserEname());
			vo.setParams("createTime", DateUtils.getNow());
			vo.setParams("noticeTitle", String.valueOf(pMap.get("noticeTitle")));
			vo.setParams("userEname", String.valueOf(pMap.get("userEname")));
			vo.setParams("noticeContent", String.valueOf(pMap.get("noticeContent")));
			vo.setParams("noticeStatus", "0");
			vo.setMethodKey("insertInfo");
			vo.setParams("sysPass", SystemTools.getInstance().getValue("push_to_dwusoft_pass"));
			ResultEntity entity = infoService.doService(vo);
			if ("true".equals(entity.getResult()) && "true".equals(String.valueOf(entity.getEntity()))) {
				rsm = new ResultMsg("T", "操作成功");
			}
		} else {
			rsm = new ResultMsg("F", "未登录，操作失败");
		}
		return rsm;
	}

	protected BaseManager getBaseManager() {
		return null;
	}

	protected String getPrefix() {
		return "info/check-info-";
	}

	protected Map<String, String> getParameterMap(HttpServletRequest request) {
		Map<String, String> map = new HashMap<String, String>();
		Set keSet = request.getParameterMap().entrySet();
		for (Iterator itr = keSet.iterator(); itr.hasNext();) {
			Map.Entry me = (Map.Entry) itr.next();
			Object ok = me.getKey();
			Object ov = me.getValue();
			String[] value = new String[1];
			if (ov instanceof String[]) {
				value = (String[]) ov;
			} else {
				value[0] = ov.toString();
			}
			if (value.length > 0) {
				if (StrUtil.isNotNull(value[0])) {
					map.put(ok.toString(), value[0]);
					request.setAttribute(ok.toString(), value[0]);
				}
			}
		}
		return map;
	}

}
